var menuChosen = Vue.extend({
  name: 'menu-chosen',
  template:[
    '<ul id="menuChosen"></ul>'
  ].join('')
});


Vue.component('menuChosen', menuChosen);

var menuChosenDom = '';

function menuChosenSelect (t) {
    menuChosenDom = $(t);
    layui.use('layer', function(){
      layerMneuChosen = layer.open({
        title: '菜单选择',
          type : 1,
          area : [ '50%', '80%' ],
          scrollbar : false,
          zIndex : 100,
          cancel: function () {
              // companyTreeModel.render();
          },
          content : $('.menu-chosen-modules')
      });
  });
}

$(function () {
  $.ajax({
      url: '/menu/list',
      success: function (res) {
          menuChosen = res.data;
          layui.use('tree', function(){
            layui.tree({
              elem: '#menuChosen'
              ,nodes: treeSpread(res.data)
              ,click: function(node){
                  menuChosenDom.val(node.name)
                  menuChosenDom.siblings('input').val(node.id)
                  menuValueChange()
                  layer.close(layerMneuChosen)
              }  
            });
          });
      }
  })
})

// 定义全部打开，暂时没有找到较好的方法
function treeSpread (list) {
    $.each(list, function (i,t) {
        t.spread = true
        t.children.length > 0 ? treeSpread(t.children) : '';
    })
    return list
}

